Ext.define('Fxt.stl-jy.Controller', {
    extend: 'Fxt.core.CrudController',
    stores: 'Fxt.stl-jy.Store',
    models: ['Fxt.stl-jy.Model'],
    views: ['Fxt.stl-jy.view.Viewport', 'Fxt.stl-jy.view.Grid', 'Fxt.stl-jy.view.QueryForm'],
    $onInit: function() {
        var me = this;
        var queryForm = me.$getViewport().$getQueryForm();
        var grid = me.$getViewport().$getGrid();
        var module = me.$getModule();
        this.control({
            'button[action=new-my-jy]': {
                click: function(btn) {
                    Fxt.requireWithLoading(module + '.view.NewJyForm', function() {
                        var form = Ext.create(module + '.view.NewJyForm');
                        var win = Ext.create('Fxt.core.view.Window', {
                            title: '创建新的交易',
                            items: [form],
                            buttons: [{
                                text: '确定',
                                handler: function() {
                                    form.getForm().submit({
                                        url: '/stl/new-my-jy',
                                        waitMsg: '正在新的交易',
                                        success: function() {
                                            win.close();
                                            Fxt.success('创建成功');
                                            me.$doQuery();
                                        },
                                        failure: Fxt._defaultSubmitFailure
                                    });
                                }
                            }]
                        });
                    });
                }
            },
            'button[action=new-sys-jy]': {
                click: function(btn) {
                    Fxt.ajax({
                        method: 'POST',
                        url: '/stl/new-sys-jy',
                        success: function (data) {
                            Fxt.success('新增交易成功');
                            me.$doQuery();
                        }
                    });
                }
            },
            'button[action=pay-jy]': {
                click: function(btn) {
                    var grid = me.$getViewport().$getGrid();
                    Fxt.selectOneRow(grid, function(record) {
                        Fxt.requireWithLoading(module + '.view.PayJyForm', function() {
                            var form = Ext.create(module + '.view.PayJyForm');
                            var win = Ext.create('Fxt.core.view.Window', {
                                title: '付款',
                                items: [form],
                                buttons: [{
                                    text: '确定',
                                    handler: function() {
                                        form.getForm().submit({
                                            url: '/stl/pay-jy',
                                            waitMsg: '正在付款',
                                            params: {
                                                jyId: record.get('jyId')
                                            },
                                            success: function() {
                                                win.close();
                                                Fxt.success('付款成功');
                                                me.$doQuery();
                                            },
                                            failure: Fxt._defaultSubmitFailure
                                        });
                                    }
                                }]
                            });
                        });
                    });
                }
            },
            'button[action=pay2-jy]': {
                click: function(btn) {
                    var grid = me.$getViewport().$getGrid();
                    Fxt.selectOneRow(grid, function(record) {
                        var jyId = record.get('jyId');
                        Fxt.ajax({
                            waitMsg: '正在生成截图',
                            method: 'GET',
                            url: '/stl/pre-pay-jy',
                            params: {
                                jyId: jyId
                            },
                            success: function(data) {
                                Fxt.requireWithLoading(module + '.view.Pay2JyForm', function() {
                                    var form = Ext.create(module + '.view.Pay2JyForm');
                                    var win = Ext.create('Fxt.core.view.Window', {
                                        title: '付款',
                                        items: [form],
                                        buttons: [{
                                            text: '重新生成',
                                            handler: function() {
                                                Fxt.ajax({
                                                    maskCmp: form,
                                                    method: 'POST',
                                                    url: '/stl/refresh-jy-jietu',
                                                    params: {
                                                        jyId: record.get('jyId')
                                                    },
                                                    success: function (data) {
                                                        form.down('image').setSrc(data);
                                                    }
                                                });
                                            }
                                        }, {
                                            text: '确定',
                                            handler: function() {
                                                form.getForm().submit({
                                                    url: '/stl/pay-jy',
                                                    waitMsg: '正在付款',
                                                    params: {
                                                        jyId: record.get('jyId')
                                                    },
                                                    success: function() {
                                                        win.close();
                                                        Fxt.success('付款成功');
                                                        me.$doQuery();
                                                    },
                                                    failure: Fxt._defaultSubmitFailure
                                                });
                                            }
                                        }]
                                    });
                                    form.getForm().setValues(data);
                                    form.down('image').setSrc(data.imageUrl);
                                });
                            }
                        });
                    });
                }
            },
            'button[action=confirm-jy]': {
                click: function(btn) {
                    var grid = me.$getViewport().$getGrid();
                    Fxt.selectOneRow(grid, function(record) {
                        Fxt.ajax({
                            method: 'POST',
                            url: '/stl/confirm-jy',
                            params: {
                                jyId: record.get('jyId')
                            },
                            success: function (data) {
                                me.$doQuery();
                            }
                        });
                    });
                }
            },
            'button[action=sync-my-account]': {
                click: function(btn) {
                    Fxt.ajax({
                        method: 'POST',
                        url: '/stl/sync-my-account',
                        success: function (data) {
                            Fxt.success('同步成功');
                        }
                    });
                }
            },
            'button[action=new-sys-jy-4-sell]': {
                click: function(btn) {
                    Fxt.ajax({
                        method: 'POST',
                        url: '/stl/new-sys-jy-4-sell',
                        success: function (data) {
                            Fxt.success('交易成功');
                            me.$doQuery();
                        }
                    });
                }
            }
        });
    }
});